﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightMiniTest
{    
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            pie_chart.Visibility = System.Windows.Visibility.Collapsed;
            bar_chart.Visibility = System.Windows.Visibility.Collapsed;              
        }

        void UC_Loaded(object sender, RoutedEventArgs e)
        {
            List<DemographData> demo = new List<DemographData>();
            demo.Add(new DemographData() { location = "USA", numStudent = int.Parse(usText.Text)});
            demo.Add(new DemographData() { location = "China", numStudent = int.Parse(chinaText.Text) });
            demo.Add(new DemographData() { location = "India", numStudent = int.Parse(indiaText.Text)});
            demo.Add(new DemographData() { location = "Korea", numStudent = int.Parse(koreaText.Text)});
            demo.Add(new DemographData() { location = "Other", numStudent = int.Parse(othersText.Text)});
            this.DataContext = demo;
            // this.Loaded += new RoutedEventHandler(UC_Loaded);
        }

        void createChart()
        {
            List<DemographData> demo = new List<DemographData>();
            demo.Add(new DemographData() { location = "USA", numStudent = int.Parse(usText.Text) });
            demo.Add(new DemographData() { location = "China", numStudent = int.Parse(chinaText.Text) });
            demo.Add(new DemographData() { location = "India", numStudent = int.Parse(indiaText.Text) });
            demo.Add(new DemographData() { location = "Korea", numStudent = int.Parse(koreaText.Text) });
            demo.Add(new DemographData() { location = "Other", numStudent = int.Parse(othersText.Text) });
            this.DataContext = demo;            
        }

        private void pieButton_Click(object sender, RoutedEventArgs e)
        {
            if (!isFormFilled())
            {
                MessageBox.Show("You must provide values for all text boxes");
                return;
            }
            try
            {
                createChart();
                pie_chart.Title = "Pie Chart for Course: " + courseText.Text;
                pie_chart.Visibility = System.Windows.Visibility.Visible;
            }
            catch (FormatException)
            {            
                MessageBox.Show("You need to enter an integer for all demographic locations");
            }            
        }

        public bool isFormFilled()
        {
            if (courseText.Text.Length <= 0 || usText.Text.Length <= 0 || chinaText.Text.Length <= 0 || indiaText.Text.Length <= 0 || koreaText.Text.Length <= 0 || othersText.Text.Length <= 0)
                return false;
            else
                return true;
        }

        private void barButton_Click(object sender, RoutedEventArgs e)
        {
            if (!isFormFilled())
            {
                MessageBox.Show("You must provide values for all text boxes");
                return;
            }
            try
            {
                createChart();
                bar_chart.Title = "Bar Chart for Course: " + courseText.Text;               
                bar_chart.Visibility = System.Windows.Visibility.Visible;
            }
            catch (FormatException)
            {
                MessageBox.Show("You need to enter an integer for all demographic locations");
            } 
        }
    }
    public class DemographData
    {
        public string location { get; set; }
        public int numStudent { get; set; }
    }
}
